home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / progjrn / pj_7_6.arc / WINDEV.ARC / WNTERM.C < prev    next >
C/C++ Source or Header  |  1989-07-30  |  5KB  |  194 lines

  1. /* 
  2.  * WNTERM Main Module
  3.  *
  4.  * Written by
  5.  * William S. Hall
  6.  * 3665 Benton Street, #66
  7.  * Santa Clara, CA 95051
  8. */
  9.  
  10. #define NOKANJI
  11. #define NOATOM
  12. #define NOSOUND
  13. #include <windows.h>
  14. #include <limits.h>
  15. #include <string.h>
  16. #include "ttycls.h"
  17. #define EXTERN
  18. #include "wnterm.h"
  19.  
  20. /* program entry point */
  21. int FAR PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  22. HANDLE hInstance, hPrevInstance;
  23. LPSTR lpszCmdLine;
  24. int cmdShow;
  25. {
  26.  
  27.     MSG msg;
  28.  
  29.   // initialize program
  30.     if (!InitProgram(hInstance,hPrevInstance, lpszCmdLine, cmdShow))
  31.     return FALSE;
  32.  
  33.   // stay in this loop until window is destroyed.
  34.     while (TRUE) {
  35.         if (PeekMessage((LPMSG)&msg,NULL,0,0,PM_REMOVE)) {
  36.         if (msg.message == WM_QUIT)
  37.         break;
  38.         TranslateMessage((LPMSG)&msg); // translate keydown/up to chars.
  39.         DispatchMessage((LPMSG)&msg);  // call the appropriate window proc.
  40.     }
  41.     else if (!IsIconic(MWnd.hWnd))       // don't read comm port if iconic.
  42.         ProcessComm();
  43.     }
  44.     return (int)msg.wParam;    // terminate program.
  45. }
  46.  
  47. /* Main window procedure */
  48. long FAR PASCAL MainWndProc(hWnd,message,wParam,lParam)
  49. HWND hWnd;
  50. unsigned message;
  51. WORD wParam;
  52. LONG lParam;
  53. {
  54.  
  55.     // get our data structure for the window.
  56.     PTTYWND pMWnd = (PTTYWND)GetWindowWord(hWnd,0); 
  57.     PAINTSTRUCT ps;
  58.     register int i;
  59.  
  60.     switch(message) {
  61.  
  62.       // control scrolling with this key.
  63.     case WM_KEYDOWN:
  64.         if (wParam == VK_SCROLL)
  65.             ScrollLock = GetKeyState(VK_SCROLL) & 1;
  66.         break;
  67.  
  68.       // process menu command.
  69.     case WM_COMMAND:
  70.         WndCommand(hWnd, wParam, lParam);
  71.         break;
  72.  
  73.     /* 
  74.        When a character is received, and send it to tty display procedure.
  75.        This is the action when terminal is off-line
  76.     */
  77.     case WM_CHAR:
  78.         HideCaret(hWnd);
  79.       // the loop is a crude way to handle repeat characters
  80.         for (i = 0; i < LOWORD(lParam); i++)
  81.             TTYDisplay(pMWnd, (short)1, (BYTE *)&wParam);
  82.         SetCaretPos(pMWnd->Pos.x, pMWnd->Pos.y);
  83.         ShowCaret(hWnd);
  84.         break;
  85.  
  86.       // recreate caret and reset its position
  87.         case WM_SIZE:
  88.         pMWnd->Width = LOWORD(lParam);
  89.         pMWnd->Height = HIWORD(lParam);
  90.         pMWnd->Pos.y = pMWnd->Height - pMWnd->CHeight - 1; 
  91.         if (hWnd == GetFocus()) {
  92.             CreateCaret(hWnd, (HBITMAP)NULL,2,pMWnd->CHeight);
  93.             SetCaretPos(pMWnd->Pos.x, pMWnd->Pos.y);
  94.             ShowCaret(hWnd);
  95.         }
  96.         break;
  97.  
  98.       // window has focus, so window can create a caret.
  99.     case WM_SETFOCUS:
  100.         CreateCaret(hWnd, (HBITMAP)NULL,2,pMWnd->CHeight);
  101.         SetCaretPos(pMWnd->Pos.x, pMWnd->Pos.y);
  102.         ShowCaret(hWnd);
  103.         break;
  104.  
  105.       // window has lost focus, so window must kill the caret.
  106.     case WM_KILLFOCUS:
  107.         HideCaret(hWnd);
  108.         DestroyCaret();
  109.         break;
  110.  
  111.       // main window now exists so do some initialization.
  112.     case WM_CREATE:
  113.         WndCreate(hWnd,(LPCREATESTRUCT)lParam);
  114.         break;
  115.  
  116.       // have to close comm port if open before killing window.
  117.     case WM_CLOSE:
  118.         if (cid >= 0) {
  119.         CloseComm(cid);
  120.         cid = INT_MIN;
  121.         }
  122.         DestroyWindow(hWnd);
  123.         break;
  124.  
  125.       // This application is being destroyed by Windows exec.
  126.     case WM_ENDSESSION:
  127.         if (wParam)        // if true, close the comm port.
  128.         if (cid >= 0) {
  129.             CloseComm(cid);
  130.             cid = INT_MIN;
  131.         }
  132.         break;
  133.  
  134.       // tell program to exit.
  135.     case WM_DESTROY:
  136.         PostQuitMessage(0);
  137.         break;
  138.  
  139.       // if iconic, paint the icon window, otherwise redraw text buffer.
  140.     case WM_PAINT:
  141.         BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
  142.         MainWndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  143.         EndPaint(hWnd, (LPPAINTSTRUCT)&ps);
  144.         break;
  145.  
  146.       // all other messages go through here.
  147.     default:
  148.         return ((long)DefWindowProc(hWnd,message,wParam,lParam));
  149.     }
  150.     return(0L);    /* this means we processed the message */
  151. }
  152.  
  153. /* 
  154.  * Subclass window procedure.  When we are on-line, all window 
  155.  *  messages come here first
  156.  */
  157. LONG FAR PASCAL MainWndSubProc(hWnd, message, wParam, lParam)
  158. HWND hWnd;
  159. unsigned message;
  160. WORD wParam;
  161. LONG lParam;
  162. {
  163.  
  164.     PTTYWND pMWnd = (PTTYWND)GetWindowWord(hWnd,0);
  165.     register int i;
  166.  
  167.   // we are only interested in these messages.
  168.     switch(message) {
  169.  
  170.       // buffer from comm port has been posted to us.
  171.     case WM_USER:
  172.         HideCaret(hWnd);
  173.         Buflen = TTYDisplay(pMWnd, (short)wParam, Buffer);
  174.         SetCaretPos(pMWnd->Pos.x, pMWnd->Pos.y);
  175.         ShowCaret(hWnd);
  176.           // if anything left over, move it to front.
  177.         if (Buflen)    
  178.         memmove(Buffer, Buffer+(int)wParam-Buflen, Buflen);
  179.         break;
  180.  
  181.       // transmit key from keyboard.  Loop is crude way to handle repeat chars.
  182.     case WM_CHAR:
  183.         for (i = 0; i < LOWORD(lParam); i++)
  184.             WriteComm(cid, (LPSTR)&wParam,1);
  185.         if (!MWnd.LocalEcho)    // if no local echo, then done
  186.         break;
  187.  
  188.     // all other messages must go thru normal channels.
  189.     default:
  190.         return(CallWindowProc(fpMainWndProc,hWnd,message,wParam,lParam));
  191.     }
  192.     return(0L);
  193. }
  194.